package com.glsc.ngateway.platform.controller.cmdb;

import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbDict;
import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbDictValue;
import com.glsc.ngateway.platform.service.cmdb.CmdbDictService;
import com.glsc.ngateway.platform.service.cmdb.CmdbDictValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字典项值接口
 */
@RestController
@RequestMapping("api/dictvalue")
@Api(value = "字典项值controller", tags = {"字典项值接口"})
public class DictValueController {

    private static Logger logger = LoggerFactory.getLogger(DictValueController.class);

    @Resource
    private CmdbDictValueService cmdbDictValueService;

    @Resource
    private CmdbDictService cmdbDictService;


    @PostMapping("/update")
    @ApiOperation(value = "更新字典项值信息", notes = "更新字典项值信息")
    public Map<String, Object> updateDictValue(@RequestBody CmdbDictValue dictValue, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        try {
            cmdbDictValueService.saveDictValue(dictValue);
            result.put("status", 0);
            result.put("message", "修改成功");
        } catch (Exception e) {
            result.put("status", -1);
            result.put("message", "修改失败:" + e.getMessage());
        }
        return result;
    }

    @PostMapping("/create")
    @ApiOperation(value = "创建字典项值信息", notes = "创建字典项值信息")
    public Map<String, Object> saveDictValue(@RequestBody CmdbDictValue dictValue, @ApiParam(required = true, value = "字典项id") @RequestParam String dictId, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();

        try {
            if (null == dictId) {
                result.put("status", -1);
                result.put("message", "字典项id不可以为空");
                return result;
            }
            CmdbDict dict = cmdbDictService.findById(Integer.valueOf(dictId));
            if (null == dict) {
                result.put("status", -1);
                result.put("message", "字典项不存在");
                return result;
            }
            cmdbDictValueService.saveDictValue(dictValue);
            List<CmdbDictValue> dictValues = dict.getDictValues();
            dictValues.add(dictValue);
            dict.setDictValues(dictValues);
            cmdbDictService.saveDict(dict);
            result.put("status", 0);
            result.put("message", "创建成功");
        } catch (Exception e) {
            result.put("status", -1);
            result.put("message", "创建失败:" + e.getMessage());
        }

        return result;
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除字典项值信息，并更新字典项", notes = "删除字典项值信息，并更新字典项")
    public Map<String, Object> deleteDictValue(@ApiParam(required = true, value = "字典项值id") @RequestParam String id,
                                               @ApiParam(required = true, value = "字典项id") @RequestParam String dictId) {
        Map<String, Object> result = new HashMap<>();
        CmdbDictValue dictValue = cmdbDictValueService.findById(Integer.valueOf(id));
        CmdbDict dict = cmdbDictService.findById(Integer.valueOf(dictId));
        List<CmdbDictValue> dictValues = dict.getDictValues();
        dictValues.remove(dictValue);
        dict.setDictValues(dictValues);
        cmdbDictService.saveDict(dict);
        cmdbDictValueService.deleteById(Integer.valueOf(id));
        result.put("status", 0);
        result.put("message", "删除成功");
        return result;
    }

    @GetMapping("/findbyid")
    @ApiOperation(value = "根据id查询字典项值信息", notes = "根据id查询字典项值信息")
    public Map<String, Object> findById(@ApiParam(required = true, value = "字典项id") @RequestParam String id,
                                        HttpServletRequest request) {

        Map<String, Object> result = new HashMap<>();
        try {
            CmdbDictValue dictValue = cmdbDictValueService.findById(Integer.parseInt(id));
            if (null != dictValue) {
                result.put("data", dictValue);
                result.put("message", "查询成功");
                result.put("status", 0);
            } else {
                result.put("status", -1);
                result.put("message", "没有查询到数据");
                return result;
            }

        } catch (Exception e) {
            logger.error("Error", e);
            result.put("message", "查询失败" + e.getMessage());
            result.put("status", -1);
        }

        return result;
    }


    @GetMapping("/list")
    @ApiOperation(value = "查询字典项列表", notes = "查询字典项列表")
    public Map<String, Object> findList(@ApiParam(required = false, value = "关键词") @RequestParam(required = false) String tag,
                                        HttpServletRequest request) {

        Map<String, Object> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        param.put("GT_dictVId", 0);
        if (null != tag && tag.length() > 0) {
            param.put("LIKE_dictValueName", tag);
        }
        List<CmdbDictValue> list = cmdbDictValueService.findList(param);
        if (null != list && list.size() > 0) {
            result.put("data", list);
            result.put("total", list.size());
            result.put("message", "查询成功");
            result.put("status", 0);
        } else {
            result.put("status", -1);
            result.put("message", "没有查询到数据");
            return result;
        }

        return result;
    }

}
